use regex::Regex;
pub fn convert_base(num_str: &str, to_base: u32) -> String {
    // 正则取出目标数字和当前进制
    let re = Regex::new(r"(\d+)\((\d+)\)").unwrap();
    
    if let Some(captures) = re.captures(num_str) {
        // 取出数字
        let number = captures.get(1).unwrap().as_str();
        let conv = captures.get(2).unwrap().as_str().parse::<u32>().unwrap();

        // 首先转换成十进制
        let decimal = u32::from_str_radix(number, conv).expect("转换失败");

        // 进制转换
        let target = to_base_n(decimal, to_base);
        target
    } else {
        String::from("提取失败")
    }
}


fn to_base_n(mut num: u32, n: u32) -> String {
    if num == 0 {
        return "0".to_string();
    }
    
    let mut base = String::new();
    while num > 0 {
        let remainder = num % n;
        base.insert(0, char::from_digit(remainder, 10).unwrap());
        num /= n;
    }
    
    let target = base + "(" + &n.to_string() + ")";
    target
}